//
//  Recent.m
//  SportSocial
//
//  Created by yull on 14/11/19.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "ChatInfo.h"
#import "FunctionMethod.h"
#import "CurrentChat.h"

@implementation ChatInfo

+ (instancetype)chatInfoWithPrivateMsg:(PrivateChatMsg *)chatMsg
{
    ChatInfo *info = [[ChatInfo alloc] init];
    info.privateId = chatMsg.roleType == ChatMsgRoleTypeReceive ? chatMsg.sendId : chatMsg.receiveId;
    info.infoType = ChatInfoTypePrivate;
    info.time = chatMsg.createTime;
    info.name = chatMsg.senderName;
    info.icon = chatMsg.icon;
    info.sex = chatMsg.sex;
    
    if (chatMsg.msgType == ChatMsgTypeText) {
        info.content = chatMsg.content;
    }else if (chatMsg.msgType == ChatMsgTypeAudio) {
        info.content = @"[语音]";
    }else{
        info.content = @"[图片]";
    }
    
    UInt64 chatId = [CurrentChat shareInstance].chatUser.userId;
    if (chatMsg.sendId == chatId || chatMsg.receiveId == chatId) {
        info.unReadCount = 0;
    }else {
        info.unReadCount = 1;
    }
    return info;
}

+ (instancetype)chatInfoWithGroupMsg:(GroupChatMsg *)chatMsg
{
    ChatInfo *info = [[ChatInfo alloc] init];
    info.groupId = chatMsg.groupId;
    info.infoType = ChatInfoTypeGroup;
    info.time = chatMsg.createTime;
    info.name = chatMsg.groupName;
    info.icon = chatMsg.groupIcon;
    info.senderName = chatMsg.senderName;
    
    if (chatMsg.msgType == ChatMsgTypeText) {
        info.content = chatMsg.content;
    }else if (chatMsg.msgType == ChatMsgTypeAudio) {
        info.content = @"[语音]";
    }else{
        info.content = @"[图片]";
    }
    
    info.content = [[NSString alloc] initWithFormat:@"%@:%@",info.senderName,info.content];
    
    UInt64 chatId = [CurrentChat shareInstance].chatGroup.group_id;
    if (chatMsg.groupId == chatId) {
        info.unReadCount = 0;
    }else {
        info.unReadCount = 1;
    }
    return info;
}

+ (instancetype)chatInfoWithResultSet:(FMResultSet *)resultSet
{
    ChatInfo *info = [[ChatInfo alloc] init];
    info.privateId = [resultSet longLongIntForColumn:@"privateId"];
    info.groupId = [resultSet longLongIntForColumn:@"groupId"];
    info.time = [resultSet longLongIntForColumn:@"time"];
    info.content = [resultSet stringForColumn:@"content"];
    info.name = [resultSet stringForColumn:@"name"];
    info.icon = [resultSet stringForColumn:@"icon"];
    info.sex = [resultSet intForColumn:@"sex"];
    info.dbId = [resultSet longLongIntForColumn:@"id"];
    info.unReadCount = [resultSet intForColumn:@"unReadCount"];
    info.infoType = [resultSet intForColumn:@"infoType"];
    info.senderName = [resultSet stringForColumn:@"senderName"];
    return info;
}
@end
